package com.xiaode.controller.app.chat;

import com.jfinal.aop.Inject;
import com.jfinal.core.Controller;
import com.jfinal.core.Path;
import com.jfinal.ext.cors.EnableCORS;
import com.jfinal.kit.Ret;
import com.xiaode.common.anotation.JSONArrayString;
import com.xiaode.util.apidoc.annotation.Api;
import com.xiaode.util.apidoc.annotation.ApiOper;
import com.xiaode.util.apidoc.annotation.ApiPara;

import javax.validation.constraints.Size;

@Path("/chatGroupManage")
@Api("群聊相关")
@EnableCORS
public class ChatGroupManageController extends Controller {

    @Inject
    ChatGroupMannageService service;

    @ApiOper("查询当前用户在群身份")
    public void getGroupUserIdentity(@Size @ApiPara(value = "chatGroupId", notes = "群聊Id") Integer chatGroupId) throws Exception {
    	Integer accountIdMy = getAttr("accountId");
        Ret ret = service.getGroupUserIdentity(accountIdMy, chatGroupId);
        renderJson(ret);
    }
    
    @ApiOper("查询当前用户群的公告")
    public void getGroupNotice(@Size @ApiPara(value = "chatGroupId", notes = "群聊Id") Integer chatGroupId) throws Exception {
    	Integer accountIdMy = getAttr("accountId");
        Ret ret = service.getGroupNotice(accountIdMy, chatGroupId);
        renderJson(ret);
    }
    
    @ApiOper("修改群聊信息")
    public void updateChatGroupData(
    		@Size @ApiPara(value = "chatGroupId", notes = "群聊Id") Integer chatGroupId,
            @ApiPara(value = "name", notes = "群聊名称") String name,
            @ApiPara(value = " notice", notes = "群公告") String notice) throws Exception {
    	Integer accountIdMy = getAttr("accountId");
        Ret ret = service.updateChatGroupData(accountIdMy, chatGroupId, name, notice);
        renderJson(ret);
    }

    @ApiOper("更换群主")
    public void updateGroupLeader(
    		@Size @ApiPara(value = "chatGroupId", notes = "群聊Id") Integer chatGroupId,
    		@Size @ApiPara(value = "userId", notes = "目标账号id") Integer accountId) throws Exception {
    	Integer accountIdMy = getAttr("accountId");
        Ret ret = service.updateGroupLeader(accountId, accountIdMy, chatGroupId);
        renderJson(ret);
    }

    @ApiOper("添加/删除群管理")
    public void updateGroupManage(
    		@Size @ApiPara(value = "chatGroupId", notes = "群聊Id") Integer chatGroupId,
    		@Size @ApiPara(value = "accountId", notes = "目标账号id") Integer accountId,
    		@ApiPara(value = "flag", notes = "添加true/删除false") Boolean flag) throws Exception {
    	Integer accountIdMy = getAttr("accountId");
        Ret ret = service.updateGroupManage(chatGroupId, accountIdMy, accountId, flag);
        renderJson(ret);
    }

    @ApiOper("全体禁言")
    public void muteAll(
    		@Size @ApiPara(value = "chatGroupId", notes = "群聊Id") Integer chatGroupId,
            @ApiPara(value = "flag", notes = "禁言true / 解禁 false") Boolean flag) throws Exception {
    	Integer accountIdMy = getAttr("accountId");
        Ret ret = service.muteAll(accountIdMy, chatGroupId, flag);
        renderJson(ret);
    }

    @ApiOper("校验群聊版本校验")
    public void checkGrooupVersion(
    		@Size @ApiPara(value = "groupId", notes = "群聊id") Integer chatGroupId,
    		@Size @ApiPara(value = "version", notes = "群聊版本") Integer version) throws Exception {
    	Integer userId = getAttr("userId");
    	Integer userType = getAttr("userType");
        Ret ret = service.checkGrooupVersion(userId, userType, chatGroupId, version);
        renderJson(ret);
    }

    @ApiOper("消息免打扰")
    public void disturb(
    		@Size @ApiPara(value = "chatGroupId", notes = "群聊Id") Integer chatGroupId,
            @ApiPara(value = "flag", notes = "打扰1 免打扰 2") Integer flag) throws Exception {
    	Integer accountIdMy = getAttr("accountId");
        Ret ret = service.disturb(accountIdMy, chatGroupId, flag);
        renderJson(ret);
    }

    @ApiOper("添加群成员")
    public void addMembers(
    		@Size @ApiPara(value = "chatGroupId", notes = "群聊Id") Integer chatGroupId,
    		@JSONArrayString @ApiPara(value = "accountIds", notes = "账号ids[1]") String accountIds) throws Exception {
        Ret ret = service.addMembers(chatGroupId, accountIds);
        renderJson(ret);
    }

    @ApiOper("踢出群聊")
    public void kickOutChatGroup(
    		@Size @ApiPara(value = "chatGroupId", notes = "群聊Id") Integer chatGroupId,
    		@JSONArrayString @ApiPara(value = "accountIds", notes = "账号ids[1]") String accountIds)throws Exception{
    	Integer accountIdMy = getAttr("accountId");
        Ret ret = service.kickOutChatGroup(chatGroupId, accountIds, accountIdMy);
        renderJson(ret);
    }

    @ApiOper("创建群聊")
    public void createChatGroup(
    		@ApiPara(value = "accountIds",notes = "账号id列表")String accountIds,
            @JSONArrayString @ApiPara(value = "chatGroupList",notes = "群聊id列表[]")String chatGroupList,
            @JSONArrayString @ApiPara(value = "departmentList",notes = "部门DTO列表 DepartmentDTO[{id:XXX,stratum:XXX}]")String departmentList)throws Exception{
    	Integer accountIdMy = getAttr("accountId");
    	Integer userType = getAttr("userType");
        Ret ret = service.createChatGroup(accountIdMy, userType, accountIds, chatGroupList, departmentList);
        renderJson(ret);
    }

    @ApiOper("解散群聊")
    public void delChatGroup(@Size @ApiPara(value = "chatGroupId",notes = "群聊id")Integer chatGroupId)throws Exception{
    	Integer accountIdMy = getAttr("accountId");
        Ret ret = service.delChatGroup(accountIdMy, chatGroupId);
        renderJson(ret);
    }

    @ApiOper("退出群聊")
    public void quitChatGroup(@Size @ApiPara(value = "chatGroupId",notes = "群聊id")Integer chatGroupId)throws Exception{
    	Integer accountId = getAttr("accountId");
        Ret ret = service.quitChatGroup(accountId,chatGroupId);
        renderJson(ret);
    }
    
    @ApiOper("添加聊天会话")
    public void addChatSession(@Size @ApiPara(value = "accountId",notes = "用户账号id")Integer accountId)throws Exception{
    	Integer accountIdMy = getAttr("accountId");
        Ret ret = service.addChatSession(accountIdMy, accountId);
        renderJson(ret);
    }

    @ApiOper("查询聊天会话")
    public void findChatSession()throws Exception{
    	Integer accountId = getAttr("accountId");
        Ret ret = service.findChatSession(accountId);
        renderJson(ret);
    }
    
    @ApiOper("删除聊天会话")
    public void delChatSession(@Size @ApiPara(value = "accountId",notes = "用户账号id")Integer accountId)throws Exception{
    	Integer accountIdMy = getAttr("accountId");
        Ret ret = service.delChatSession(accountIdMy, accountId);
        renderJson(ret);
    }

    @ApiOper("查询年级公告")
    public void findGradeNotice(
            @Size @ApiPara(value = "page",notes = "当前页")Integer page,
            @Size @ApiPara(value = "limit",notes = "每页大小")Integer limit,
            @ApiPara(value = "state",notes = "状态")Integer state)throws Exception{
    	Integer userId = getAttr("userId");
    	Integer userType = getAttr("userType");
        Ret ret = service.findGradeNotice(userId,userType,page,limit,state);
        renderJson(ret);
    }

    @ApiOper("添加年级公告")
    public void addGradeNotice(@Size @ApiPara(value = "notice",notes = "内容")String notice)throws Exception{
    	Integer userId = getAttr("userId");
    	Integer userType = getAttr("userType");
        Ret ret = service.addGradeNotice(userId,userType,notice);
        renderJson(ret);
    }

    @ApiOper("修改年级公告")
    public void updateGradeNotice(
            @Size @ApiPara(value = "notice",notes = "年级id")String notice,
            @Size @ApiPara(value = "noticeId",notes = "公告Id")Integer noticeId)throws Exception{
    	Integer userId = getAttr("userId");
    	Integer userType = getAttr("userType");
        Ret ret = service.updateGradeNotice(userId,userType,notice,noticeId);
        renderJson(ret);
    }

    @ApiOper("删除年级公告")
    public void delGradeNotice(@Size @ApiPara(value = "gradeId",notes = "年级id")Integer noticeId)throws Exception{
    	Integer userId = getAttr("userId");
    	Integer userType = getAttr("userType");
        Ret ret = service.delGradeNotice(userId,userType,noticeId);
        renderJson(ret);
    }

    @ApiOper("查看年级公告")
    public void lookOverGradeNotice(@Size @ApiPara(value = "noticeLogId",notes = "公告Id")Integer noticeLogId){
        Ret ret = service.lookOverGradeNotice(noticeLogId);
        renderJson(ret);
    }
    
    
    
    
    
    
    
    /**
	 * 获取群聊成员
	 */
    @ApiOper("获取群聊成员")
	public void findMembersByGroupId(@Size @ApiPara(value = "groupId",notes = "群聊id")Integer groupId)throws Exception{
    	Integer accountId = getAttr("accountId");
		Ret ret = service.findMembersByGroupId(accountId, groupId);
		renderJson(ret);
	}

}
